﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_ReturnEdit : DevExpress.XtraEditors.XtraForm
    {
        public int Id;
        public DateTime Date;
        public string ToolId;
        public string StatusName;
        public int Quantity;
        public string Staff;
        public string Machine;
        public string Note;

        private bool firstLoadDone;
        private int number;

        private DataTable dt = new DataTable();
        private ucReturn parent;

        public frm_ReturnEdit(ucReturn fReturn)
        {
            InitializeComponent();
            btTAdd.Image = ERP_RORZE_TOOL.Properties.Resources.accept;
            btTCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fReturn;
            this.CancelButton = this.btTCancel;
        }
        
        private void XtraForm1_Load(object sender, EventArgs e)
        {
            dtpTDate.Value = Date;
            txtTQuantity.Text = Quantity.ToString();
            memoTNote.Text = Note;
            BindDataStaff();
            BindDataMachine();
            BindDataStatus();
            BindDataTool();

            
        }

        private void btTUpdate_Click_1(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void btTCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbToolID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0] && firstLoadDone)
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);
                if (number == 0)
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineNotBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineBorrow, cmbMachine.SelectedValue, number.ToString(), cmbToolID.SelectedValue), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        #region Private methods

        private bool GetDataTool(Return o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Return.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbStaff.SelectedValue) || cmbStaff.SelectedItem == cmbStaff.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Return.StaffCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbMachine.SelectedValue) || cmbMachine.SelectedItem == cmbMachine.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Return.MachineCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolID.SelectedValue) || cmbToolID.SelectedItem == cmbToolID.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Return.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbStatus.SelectedValue) || cmbStatus.SelectedItem == cmbStatus.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Return.StatusCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Return.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Return.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {

                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);

                if ((cmbToolID.SelectedValue.ToString() != ToolId) && number < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.WrongQuantity, txtTQuantity.Text, number.ToString()), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Focus();
                    return false;
                }
                else
                    if ((cmbToolID.SelectedValue.ToString() != ToolId) && number > int.Parse(txtTQuantity.Text))
                    {
                        MessageBox.Show(string.Format(ToolConstants.Message.Return.ResultQuantity, cmbMachine.SelectedValue, number.ToString(), cmbToolID.SelectedValue, txtTQuantity.Text, number - int.Parse(txtTQuantity.Text)), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                        if ((cmbToolID.SelectedValue.ToString() == ToolId) && (int.Parse(txtTQuantity.Text) - Quantity) > number)
                        {
                            MessageBox.Show(string.Format(ToolConstants.Message.Return.WrongQuantity2, Quantity, number.ToString(), txtTQuantity.Text), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtTQuantity.Text = "";
                            txtTQuantity.Focus();
                            return false;

                        }

            }
            #endregion

            o.ID = Id;
            o.Date = dtpTDate.Value;
            o.ToolID = cmbToolID.SelectedValue.ToString();
            o.StatusName = cmbStatus.SelectedText;
            o.StaffID = cmbStatus.SelectedValue.ToString();
            o.Quantity = int.Parse(txtTQuantity.Text);
            o.StaffID = cmbStaff.SelectedValue.ToString();
            o.MachineID = cmbMachine.SelectedValue.ToString();
            o.Note = memoTNote.Text;

            return true;
        }

        private void BindDataTool()
        {
            ToolHelper helper = new ToolHelper(new Tools());
            dt = helper.GetAllByToolType();
            DataRow row = dt.NewRow();


            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
            cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

            cmbToolID.SelectedValue = ToolId;
            if (cmbToolID.SelectedItem == null)
            {
                cmbToolID.SelectedItem = cmbToolID.Items[0];
            }
            firstLoadDone = true;
        }

        private void BindDataStaff()
        {
            StaffHelper helper = new StaffHelper(new Staff());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Staff.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Staff.FieldName.StaffID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbStaff.DataSource = dt;
            cmbStaff.DisplayMember = ToolConstants.Entities.Staff.FieldName.FullName;
            cmbStaff.ValueMember = ToolConstants.Entities.Staff.FieldName.StaffID;

            cmbStaff.SelectedValue = Staff;

            if (cmbStaff.SelectedItem == null)
            {
                cmbStaff.SelectedItem = cmbStaff.Items[0];
            }
        }

        private void BindDataMachine()
        {
            MachineHelper helper = new MachineHelper(new Machine());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Machine.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Machine.FieldName.MachineID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMachine.DataSource = dt;
            cmbMachine.DisplayMember = ToolConstants.Entities.Machine.FieldName.FullName;
            cmbMachine.ValueMember = ToolConstants.Entities.Machine.FieldName.MachineID;

            cmbMachine.SelectedValue = Machine;
            if (cmbMachine.SelectedItem == null)
            {
                cmbMachine.SelectedItem = cmbMachine.Items[0];
            }
        }

        private void BindDataStatus()
        {
            ToolStatusHelper helper = new ToolStatusHelper(new ToolStatus());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.ToolStatus.FieldName.StatusName] = "Select...";
            dt.Rows.InsertAt(row, 0);

            cmbStatus.DataSource = dt;
            cmbStatus.DisplayMember = ToolConstants.Entities.ToolStatus.FieldName.StatusName;
            cmbStatus.ValueMember = ToolConstants.Entities.ToolStatus.FieldName.StatusName;

            cmbStatus.SelectedValue = StatusName.ToString();
            if (cmbStatus.SelectedItem == null)
            {
                cmbStatus.SelectedItem = cmbStatus.Items[0];
            }
        }

        private void UpdateData()
        {
            ReturnHelper helper = new ReturnHelper(new Return());
            if (GetDataTool(helper.ValueObject))
            {



                if (helper.Update() > 0)
                {

                    if (!Utilities.MessageConfirmationYesNo(ToolConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.UpdateFailed);
                }
                parent.BindDatatoGrid();

            }
        }

        #endregion



    }
}